package indi.cloud.common.elasticsearch.core;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;


@Getter
@Setter
public class EsPageable {
    private Integer page = 1;
    private Integer size = 10;
    private String orderBy = "";
    private String order = "";

    public enum OrderEnum {
        /**
         * 正序
         */
        ASC("ASC"),
        /**
         * 倒叙
         */
        DESC("DESC");
        /**
         * 值
         */
        private String value;

        OrderEnum(String value) {
            this.value = value;
        }
    }


    public static Sort getQuerySort(EsPageable pageable) {
        Sort sort = null;
        if (StringUtils.isNotBlank(pageable.getOrderBy()) && pageable.getOrder() != null) {
            if (StringUtils.equalsIgnoreCase(pageable.getOrder(), EsBaseReq.OrderEnum.ASC.name())) {
                sort = Sort.by(Sort.Order.asc(pageable.getOrderBy()));
            } else {
                sort = Sort.by(Sort.Order.desc(pageable.getOrderBy()));
            }
        }
        return sort == null ? Sort.unsorted() : sort;
    }

    public static Pageable getQueryPageable(EsPageable pageable) {
        int page = pageable.getPage() - 1;
        Integer size = pageable.getSize();
        return PageRequest.of(page, size);
    }

}
